package org.madmac.controller.faixaIp;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.madmac.faixaIp.FaixaIp;
import org.madmac.faixaIp.FaixaIpBo;
import org.madmac.faixaIp.FaixaIpDao;
import org.madmac.servlet.action.Action;
import org.madmac.servlet.form.Form;

/**
 *
 * @author atila camurca
 */
public class FaixaIpServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
			  throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			/* 
			out.println("<html>");
			out.println("<head>");
			out.println("<title>Servlet FaixaIpServlet</title>");
			out.println("</head>");
			out.println("<body>");
			out.println("<h1>Servlet FaixaIpServlet at " + request.getContextPath () + "</h1>");
			out.println("</body>");
			out.println("</html>");
			 */
		} finally {
			out.close();
		}
	}

	/**
	 * Handles the HTTP <code>GET</code> method.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			  throws ServletException, IOException {
		try {
			if (request.getParameter("form") != null) {
				if (Form.ADD.equals(Form.valueOf(request.getParameter("form")))) {
					String ipValido = FaixaIpDao.getInstance().findNextIp();
					request.setAttribute("ipValido", ipValido);
					request.setAttribute("form", Form.ADD);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("error", e);
			request.setAttribute("action", Action.ERROR);
		} finally {
			RequestDispatcher dispatcher = request.getRequestDispatcher("faixa.ip/faixa.ip.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			  throws ServletException, IOException {
		try {
			if (request.getParameter("id") == null) {
				FaixaIp faixa = new FaixaIp();
				faixa.setDescricao(request.getParameter("descricao"));
				faixa.setFaixaIni(request.getParameter("faixa_ini"));
				faixa.setFaixaFim(request.getParameter("faixa_fim"));

				// chamar BO
				Integer id = FaixaIpBo.getInstance().validateAndCreate(faixa);
				FaixaIp fi = FaixaIpDao.getInstance().findById(id);
				request.setAttribute("faixa", fi);
				request.setAttribute("action", Action.LIST);
			}
		} catch (Exception e) {
			request.setAttribute("error", e);
			request.setAttribute("action", Action.ERROR);
		} finally {
			RequestDispatcher dispatcher = request.getRequestDispatcher("faixa.ip/faixa.ip.jsp");
         dispatcher.forward(request, response);
		}
	}

	/**
	 * Returns a short description of the servlet.
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}
}
